package com.aletto.general_store.utils;

import android.util.Log;

import com.aletto.general_store.data.entities.WalletAccount;
import com.centerprime.binance_smart_chain_sdk.util.BalanceUtils;

import org.consenlabs.tokencore.wallet.Identity;
import org.consenlabs.tokencore.wallet.Wallet;
import org.consenlabs.tokencore.wallet.WalletManager;
import org.consenlabs.tokencore.wallet.model.BIP44Util;
import org.consenlabs.tokencore.wallet.model.ChainType;
import org.consenlabs.tokencore.wallet.model.Metadata;
import org.consenlabs.tokencore.wallet.model.Network;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.http.HttpService;

import java.math.BigInteger;
import java.util.HashMap;

public class WalletHelper {

    /**
     * walletFormMnemonic("test1", "123456", "position orchard cabbage foil this embody fork eternal like ask describe sport");
     * walletFormPrivateKey("test1", "123456", "e679466fd368162eb5269f5408b4371d40ed11b5b7425dff55077ca5fa929420");
     */

    private final static String DEFAULT_PASSWORD = "123456", PASSWORD_HINT = "password_hint";

    // 根据助记词创建钱包
    public static WalletAccount walletFormMnemonic(String name, String mnemonic) {

        try {

            Identity.createIdentity(name, DEFAULT_PASSWORD, PASSWORD_HINT, Network.MAINNET, Metadata.P2WPKH);

            Wallet wallet = WalletManager.importWalletFromMnemonic(
                    new Metadata(
                            ChainType.ETHEREUM,
                            Network.MAINNET,
                            name,
                            PASSWORD_HINT
                    ),
                    mnemonic.trim(),
                    BIP44Util.ETHEREUM_PATH,
                    DEFAULT_PASSWORD,
                    true
            );

            Log.i("WALLET", "========================================================");
            Log.i("WALLET", "0x" + wallet.getAddress());
            Log.i("WALLET", "exportPrivateKey: " + wallet.exportPrivateKey(DEFAULT_PASSWORD));
            Log.i("WALLET", "Keystore: " + WalletManager.exportKeystore(wallet.getId(), DEFAULT_PASSWORD));
            Log.i("WALLET", "Pub:" + wallet.getEncXPub());
            Log.i("WALLET", "Mnemonic:" + WalletManager.exportMnemonic(wallet.getId(), DEFAULT_PASSWORD).getMnemonic());
            Log.i("WALLET", "========================================================");

            return new WalletAccount(
                    wallet.getAddress(),
                    mnemonic,
                    wallet.exportPrivateKey(DEFAULT_PASSWORD),
                    WalletManager.exportKeystore(wallet.getId(), DEFAULT_PASSWORD),
                    wallet.getEncXPub()
            );

        } catch (Exception exception) {

            return null;
        }

    }

    // 根据私钥获取钱包
    public static WalletAccount walletFormPrivateKey(String name, String privateKey) {

        try {

            Identity.createIdentity(name, DEFAULT_PASSWORD, PASSWORD_HINT, Network.MAINNET, Metadata.P2WPKH);

            Wallet wallet = WalletManager.importWalletFromPrivateKey(
                    new Metadata(
                            ChainType.ETHEREUM,
                            Network.MAINNET,
                            name,
                            PASSWORD_HINT
                    ),
                    privateKey,
                    DEFAULT_PASSWORD,
                    true
            );

            Log.i("WALLET", "========================================================");
            Log.i("WALLET", "0x" + wallet.getAddress());
            Log.i("WALLET", "Keystore: " + WalletManager.exportKeystore(wallet.getId(), DEFAULT_PASSWORD));
            Log.i("WALLET", "Pub:" + wallet.getEncXPub());
            Log.i("WALLET", "========================================================");

            return new WalletAccount(
                    wallet.getAddress(),
                    WalletManager.exportKeystore(wallet.getId(), DEFAULT_PASSWORD),
                    wallet.getEncXPub(),
                    privateKey
            );

        } catch (Exception exception) {

            return null;
        }

    }


    private void balance() {
        boolean isMainNet = false;
        String mainnetInfuraUrl = "https://binance.llamarpc.com";
        String address = "0x95248dd508b4740654b6d36334163531b5b1eac4";

        Web3j web3j = Web3j.build(new HttpService(mainnetInfuraUrl, false));

        try {
            BigInteger valueInWei = web3j
                    .ethGetBalance(address, DefaultBlockParameterName.LATEST)
                    .send()
                    .getBalance();
            HashMap<String, Object> body = new HashMap<>();
            body.put("action_type", "COIN_BALANCE");
            body.put("wallet_address", address);
            body.put("network", isMainNet ? "MAINNET" : "TESTNET");
            body.put("balance", BalanceUtils.weiToEth(valueInWei));
            body.put("status", "SUCCESS");
            //sendEventToLedger(body, context);
            //BalanceUtils.weiToEth(valueInWei);
            Log.i("WALLET", "========================================================");
            Log.i("WALLET", "valueInWei:" + BalanceUtils.weiToEth(valueInWei));

            Log.i("WALLET", "========================================================");
        } catch (Exception e) {
            // e.printStackTrace();
        }

    }

}
